﻿using FlickrNet;
using Lsk.Business;
using Lsk.Business.DTO;
using Lsk.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace lsk.Web.Api
{
    public class SaleLetController : ApiController
    {
        public PhotoInfo base64ToImage(string data)
        {
            PhotoManager photMgr = new PhotoManager();
            AW_Picture picture = new AW_Picture();
            picture.PictureId = Guid.NewGuid();
            picture.PictureDesc = "SaleLet";
            picture.Picture = "SaleLet";
            picture.CreateDate = DateTime.Now;
            picture.PropertyId = new Guid();
           
            var base64Data = Regex.Match(data, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
            var binData = Convert.FromBase64String(base64Data);
            using (var stream = new MemoryStream(binData))
            {
                PhotoInfo info = photMgr.Upload(stream, picture);
                picture.PhotoId = info.PhotoId;
                picture.MediumUrl = info.MediumUrl;
                picture.pictureURL = info.OriginalUrl;
                picture.SmallUrl = info.SmallUrl;
                picture.LargeUrl = info.LargeUrl;
                return info;

            }


        }
        [HttpPost]
        [Authorize]
        public async Task<HttpResponseMessage> save()
        {
            var root = HttpContext.Current.Server.MapPath("~/");
            var provider = new MultipartFormDataStreamProvider(root);
            String programTrack = "";
            try
            {
                PhotoInfo photo = new PhotoInfo();
                // Read the form data.             
                await Request.Content.ReadAsMultipartAsync(provider);              
                var data = provider.FormData.GetValues("property")[0];
                UserSubmitInfoParameter entry = JsonConvert.DeserializeObject<UserSubmitInfoParameter>(provider.FormData.GetValues("property")[0]);
                // get all images
                var images = provider.FormData.GetValues("image");
                List<PhotoInfo> photos = new List<PhotoInfo>();
                // Retrieve all images
                foreach (var image in images)
                {
                    string base64Image = image.ToString();
                    photo = base64ToImage(base64Image);
                    photos.Add(photo);
                }
                for (int i=1; i<= photos.Count; i++)
                {
                    if (i==1)
                    {
                        entry.photoId1 = photos[0].PhotoId;
                        entry.url1 = photos[0].OriginalUrl;
                    }
                    if (i == 2)
                    {
                        entry.photoId2 = photos[1].PhotoId;
                        entry.url2 = photos[1].OriginalUrl;

                    }
                    if( i== 3)
                    {
                        entry.photoId3 = photos[2].PhotoId;
                        entry.url3 = photos[2].OriginalUrl;
                    }

                }

                SaleLet manager = new SaleLet();
                string output = manager.SaveSaleLet(entry);
               
                
                return Request.CreateResponse(HttpStatusCode.OK, output);

            }


            catch (Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error Upload Photo!" + programTrack); // + "  \n" + e.InnerException.Message.ToString() +" \n" + e.InnerException.StackTrace.ToString());
            }
        }
    }
}
